//
//  MCChooseCitysView.swift
//  StoreManage
//
//  Created by MC on 2018/9/21.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCChooseCitysViewClosure = ([MCChooseAreaCityModel],IndexPath) -> Void



class MCChooseCitysView: UIView {

    
    var closure : MCChooseCitysViewClosure?
    
    var indexPath = IndexPath()
    
    
    var model = MCChooseAreaModel() {
        didSet {
            provinceLabel.text = model.proName
            cityModels = model.allCityModels
            
            for model in cityModels {
                model.b = false
            }
            
            selectedAllButton.isSelected = false
            tableView.reloadData()
        }
    }
    
    
    private var cityModels = [MCChooseAreaCityModel]()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
       
        self.addSubview(bgView)
        bgView.addSubview(whiteView)
        whiteView.addSubview(selectedAllButton)
        whiteView.addSubview(sureButton)
        whiteView.addSubview(provinceLabel)
        whiteView.addSubview(tableView)
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        bgView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self)
        }
        
        whiteView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(bgView)
            make.bottom.equalTo(10)
            make.height.equalTo(MCScreenHeight * 0.5)
        }
        
        selectedAllButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.top.equalTo(10)
            make.size.equalTo(CGSize.init(width: 60, height: 40))
        }
        
        sureButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.top.equalTo(10)
            make.size.equalTo(CGSize.init(width: 40, height: 40))
        }
        
        
        provinceLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(whiteView)
            make.centerY.equalTo(selectedAllButton)
        }
        
        tableView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(0)
            make.top.equalTo(selectedAllButton.snp.bottom).offset(10)
            make.bottom.equalTo(-10)
        }
    }
    
    @objc func dismissSelf() {
        self.removeFromSuperview()
    }
    
    @objc func emptyEvent() {}
    
    @objc func selectedAllButtonClicked() {
        
        selectedAllButton.isSelected = !selectedAllButton.isSelected
        
        for model in cityModels {
            model.b = selectedAllButton.isSelected ? true : false
        }
        
        tableView.reloadData()
    }
    
    @objc func sureButtonClicked() {
        dismissSelf()
        
        // 点击了确定，将选中的城市的model，赋值到列表的选中城市数组中。
        
        model.b = true
        
        let arrayM = NSMutableArray()
        for model in cityModels {
            if model.b {
                arrayM.add(model)
            }
        }
        
        closure?(arrayM as! [MCChooseAreaCityModel],indexPath)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.init(white: 0, alpha: 0.4)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(dismissSelf))
        view.addGestureRecognizer(tap)
        return view
    }()

    lazy var provinceLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(16)
        label.textColor = UIColor.MCWords
        return label
    }()
    
    lazy var whiteView: UIView = {
        let view = UIView()
        view.layer.cornerRadius = 10
        view.layer.masksToBounds = true
        view.backgroundColor = UIColor.white
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(emptyEvent))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    lazy var selectedAllButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitle(" 全选", for: .normal)
        button.setTitleColor(UIColor.MCWords, for: .normal)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_notSelected"), for: .normal)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_selected"), for: .selected)
        button.addTarget(self, action: #selector(selectedAllButtonClicked), for: .touchUpInside)
        return button
    }()
    
    lazy var sureButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitle("确定", for: .normal)
        button.setTitleColor(UIColor.MCBlue, for: .normal)
        button.addTarget(self, action: #selector(sureButtonClicked), for: .touchUpInside)

        return button
    }()
    
    lazy var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()

}


extension MCChooseCitysView: UITableViewDelegate,UITableViewDataSource {
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cityModels.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCChooseCitysCell.customCell(tableView: tableView)
        cell.model = cityModels[indexPath.row]
        return cell
    }
}



// Cell
class MCChooseCitysCell: MCTableViewCell {
    
    var model = MCChooseAreaCityModel() {
        didSet {
            nameLabel.text = model.cityName
            statusButton.isSelected = model.b
        }
    }
    
    
    public class func customCell(tableView:UITableView) -> MCChooseCitysCell {
        
        let reuseIdentifier = "cell";
        var cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
        
        cell = MCChooseCitysCell(style: .default, reuseIdentifier: reuseIdentifier)
        
        return cell as! MCChooseCitysCell
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.addSubview(lineView)
        self.addSubview(statusButton)
        self.addSubview(nameLabel)

    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(1)
        }
        
        statusButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.centerY.equalTo(self)
            make.height.width.equalTo(40)
        }

        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.centerY.equalTo(self)
        }

    }
    
    @objc func statusButtonClicked() {
        statusButton.isSelected = !statusButton.isSelected
        model.b = statusButton.isSelected
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var statusButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_notSelected"), for: .normal)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_selected"), for: .selected)
        button.addTarget(self, action: #selector(statusButtonClicked), for: .touchUpInside)
        return button
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.MCWords
        label.text = "XXXX"
        return label
    }()

}


